local internet__zhefanx = fk.CreateSkill({
  name = "internet__zhefanx",
  tags = { },
})




internet__zhefanx:addEffect(fk.EventPhaseStart, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    if not (player:hasSkill(self.name) and target == player and player.phase == Player.Finish) then
      return false
    end
    if not ((player.general == "internet__sanli" or player.deputyGeneral == "internet__sanli") or 
            player:getMark("@evol__universe_will") > 0) then 
      return false 
    end
    
    local has_hook = false
    for _, p in ipairs(player.room:getAlivePlayers()) do
      if p:getMark("@internet__tanshex_hook") > 0 then
        has_hook = true
        break
      end
    end
    return has_hook
  end,
  
  on_cost = function(self, event, target, player, data)
    local room = player.room
    return room:askToSkillInvoke(player, {
      skill_name = self.name, 
      data = data
    })
  end,
  
  on_use = function(self, event, target, player, data)
    local room = player.room
    local hook_positions = {}
    for _, p in ipairs(room:getAlivePlayers()) do
      if p:getMark("@internet__tanshex_hook") > 0 and p.seat ~= player.seat then
        table.insert(hook_positions, p.seat)
      end
    end
    
    if #hook_positions == 0 then
      room:sendLog{type = "#ZhefanNoValidTarget", from = player.id}
      return false
    end
    
    local current_pos = player.seat
    local n = #room.players
    local farthest_pos = nil
    local max_distance = 0
    
    for _, pos in ipairs(hook_positions) do
      local clockwise_distance = (pos - current_pos + n) % n
      if clockwise_distance == 0 then
        clockwise_distance = n
      end
      local counter_clockwise_distance = (current_pos - pos + n) % n
      if counter_clockwise_distance == 0 then
        counter_clockwise_distance = n
      end
      local dist = math.min(clockwise_distance, counter_clockwise_distance)
      if dist > max_distance then
        max_distance = dist
        farthest_pos = pos
      end
    end
    
    if not farthest_pos then
      return false
    end
    
    local farthest_player = room:getPlayerBySeat(farthest_pos)
    if farthest_player then
      room:sendLog{type = "#ZhefanFarthestPoint", from = player.id, to = {farthest_player.id}}
    end
    
    local choices = {"clockwise", "counter_clockwise"}
    local choice = room:askToChoice(player, {
      choices = choices,
      skill_name = self.name,
      prompt = "#internet__zhefanx-choose::"..(farthest_player and farthest_player.general or ""),
      optional = false
    })
    
    local is_clockwise = (choice == "clockwise")
    local path_positions = {}
    local temp_pos = current_pos
    
    while temp_pos ~= farthest_pos do
      if is_clockwise then
        temp_pos = (temp_pos % n) + 1
      else
        temp_pos = ((temp_pos - 2) % n) + 1
      end
      table.insert(path_positions, temp_pos)
    end
    
    local target_player = room:getPlayerBySeat(farthest_pos)
    if target_player then
      room:sendLog{type = "#ZhefanMove", from = player.id, to = {target_player.id}}
      if is_clockwise then
        room:sendLog{type = "#ZhefanClockwise"}
      else
        room:sendLog{type = "#ZhefanCounterClockwise"}
      end
      
      room:swapSeat(player, target_player)
      local affected_players = {}
      
      for _, p in ipairs(room:getAlivePlayers()) do
        if p ~= player and (table.contains(path_positions, p.seat) or 
                          p.seat == current_pos or p.seat == farthest_pos) then
          table.insert(affected_players, p)
          local equips = p:getCardIds(Player.Equip)
          if #equips > 0 then
            room:sendLog{type = "#ZhefanDiscard", from = player.id, to = {p.id}}
            room:throwCard(equips, internet__zhefanx.name, p, player)
          end
        end
      end
      
      if player:hasSkill("internet__yinbaox") then
        for _, p in ipairs(affected_players) do
          if p.dead then goto continue end
          room:sendLog{type = "#ZhefanYinbao", from = player.id, to = {p.id}}
          
          if not p:isNude() then
            local discard_num = math.random(1, 2)
            local prompt = "音爆：请弃置"..discard_num.."张牌，否则将受到1点随机属性伤害"
            local cards = room:askToDiscard(p, {
              min_num = discard_num,
              max_num = discard_num,
              optional = true,
              skill_name = "internet__yinbaox",
              pattern = ".",
              prompt = prompt
            })
            
            if #cards > 0 then
              goto continue
            end
          end
          
          if room:askToSkillInvoke(player, {
            skill_name = "internet__yinbaox", 
            prompt = "#internet__yinbaox-effect::"..p.id
          }) then
            room:sendLog{type = "#YinbaoDamage", from = player.id, to = {p.id}}
            local damage_types = {fk.NormalDamage, fk.FireDamage, fk.ThunderDamage, fk.IceDamage}
            local random_type = damage_types[math.random(1, #damage_types)]
            room:damage({
              from = player,
              to = p,
              damage = 1,
              damageType = random_type,
              skillName = "internet__yinbaox",
            })
          end
          
          ::continue::
        end
      end
    end
    
    return true
  end,
})

Fk:loadTranslationTable{
  ["internet__zhefanx"] = "折返",
  [":internet__zhefanx"] = "共鸣技，结束阶段，你可以选择顺时针或逆时针回到最远「钩爪残影」位置，途经所有其他角色（包括起始位和目标位）弃置装备区所有牌并触发「音爆」效果。",
  ["#internet__zhefanx-choose"] = "折返：请选择回到最远「钩爪残影」位置的方向",
  ["#ZhefanMove"] = "%from 发动了【折返】技能，移动到 %to 的位置",
  ["#ZhefanClockwise"] = "移动方向：顺时针",
  ["#ZhefanCounterClockwise"] = "移动方向：逆时针",
  ["#ZhefanDiscard"] = "%from 的【折返】技能迫使 %to 弃置了装备区的所有牌",
  ["#ZhefanYinbao"] = "%from 的「折返」联动「音爆」对 %to 生效",
  ["#ZhefanNoValidTarget"] = "%from 的【折返】技能没有找到合适的移动目标",
  ["#ZhefanFarthestPoint"] = "%from 的【折返】技能选择了最远的钩爪残影点：%to 的位置",
}

return internet__zhefanx